<?php
namespace app\staff\controller;
use think\Request;
use app\common\controller\Common;
class Repair extends Common
{
	protected $model = null;
	protected $keyword_search = ['repair_id','repair_sn'];
	protected $order = 'repair_id desc';
	protected $royalty_money = null;//维修提成金额字段-管理者类型（登陆者or查看者）

	public function _initialize()
	{
		parent::_initialize();
		$this->model = model('repair/Repair');
	}
	/**
	 * 维修记录
	 */
	public function index()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');
				$page = isset($data['page']) ? $data['page'] : 1;
				$row = isset($data['limit']) ? $data['limit'] : $this->row;
				$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
				$search = $this->keyword_search($search_arr);
				$order = $this->order;
				$where = $this->handleList($data);
				$list = $this->model->listData($where, $order, $page, $row, $search);
				$count = $this->model->listCount($where, $order, $search);
				$res['list'] = collection($list)->toArray();
				$res['count'] = model('RepairCount')->repairCount($where, $order, $search, $this->royalty_money);
				return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $res, $page, $row);
			}
			return $this->fetch('');
		}
	}
	/**
	 * 维修详情
	 */
	public function details($ids = null)
	{
		$row = $this->model->get($ids);
		if(Request()->isGet()){
			$this->assign('row',$row);
			return $this->fetch('');
		}
	}
	/**
	* 员工维修业绩
	*/
	public function statistics()
	{
		if(Request()->isGet()){
			return $this->fetch('');
		}
	}
	/************************************ 私有 **************************************/
	/**
	 * 维修单列表查询条件处理
	 * @param array $data 查询参数
	 */
	protected function handleList($data)
	{
		$where = '';
		/*@已完成-付款并取机*/
		$where = '`state` = 8';
		/*@登陆者判断*/
		if(isset($data['ids']) && $data['ids'] != '' && isset($data['type']) && $data['type'] != ''){
			$admin_type = $data['type'];
			$admin_id = $data['ids'];
		}else{
			$admin_type = session('admin.admin_type');
			$admin_id = session('admin.admin_id');
		}
		
		if($admin_type == 2){
			$str = '`admin_id` = '.$admin_id;
			$this->royalty_money = 'sale_t_money';
		}else if($admin_type == 3){
			$str = '`technician_id` = '.$admin_id;
			$this->royalty_money = 'repair_t_money';
		}else{
			return false;
		}
		$where .= $where == '' ? $str : ' and ' . $str;
		/*@时间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$str = '`addtime` >= ' . strtotime($data['start']) . ' and `addtime` < ' . strtotime($data['end']);
			$where .= $where == '' ? $str : ' and ' . $str;
		}else if(isset($data['start']) && $data['start'] != ''){
			$str = '`addtime` >= ' . strtotime($data['start']);
			$where .= $where == '' ? $str : ' and ' . $str;
		}else if(isset($data['end']) && $data['end'] != ''){
			$str = '`addtime` < ' . strtotime($data['end']);
			$where .= $where == '' ? $str : ' and ' . $str;
		}
		return $where;
	}
}